package com.migo.api;

import com.migo.annotation.IgnoreAuth;
import com.migo.common.RedisCommonTools;
import com.migo.entity.SmsCountryEntity;
import com.migo.service.SmsCountryService;
import com.migo.utils.R;
import com.migo.utils.redis.RedisCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author powervision
 * @email wes.wang@powervision.me
 * @date 2019-11-01 14:44:55
 */
@RestController
@RequestMapping("/api")
public class ApiSmsCountryController {
    @Autowired
    private SmsCountryService smsCountryService;


    @Autowired
    private RedisCacheUtil<List<SmsCountryEntity>> redisCacheUtil;

    /**
     * 列表
     */
    @IgnoreAuth
    @RequestMapping(value = "countryList", produces = "application/json;charset=utf-8;")
    public R countryList(@RequestParam Map<String, Object> params) {
        List<SmsCountryEntity> smsCountryList = null;
        String key = "countryList";
        if (redisCacheUtil.hashKey(key)) {
            smsCountryList = redisCacheUtil.getCacheList(key);
            if (params.containsKey("flush")) {  //刷新标志
                redisCacheUtil.deleteObject(key);
            }
        } else {
            //查询数据库写入redis
            synchronized (this) {
                smsCountryList = smsCountryService.queryList(params);
                redisCacheUtil.setCacheList(key, smsCountryList);
            }
        }
        return R.ok().put("data", smsCountryList);
    }


}
